Fix integer overflow in BytesRequiredForTensor and TfLiteEvalTensorByteLength - #3637
Open
TristanInSec wants to merge 1 commit into
Open
Fix integer overflow in BytesRequiredForTensor and TfLiteEvalTensorByteLength#3637TristanInSec wants to merge 1 commit into
TristanInSec wants to merge 1 commit into
Conversation
…teLength Both functions compute tensor byte size using int32 element_count, which overflows when model-supplied dimensions are large (e.g., 65536 x 65536 wraps to 0). The undersized allocation leads to heap buffer overflow when kernels write tensor data using actual dimensions. Fix: use size_t for element_count and add overflow detection via division check after each multiplication. Return kTfLiteError on overflow or non-positive dimensions. This runs during Setup (model loading), consistent with the Error Handling Guide's recommendation to validate model-provided parameters early. The fix also rejects dimensions <= 0, which are semantically invalid for tensor shapes and would produce incorrect byte calculations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
BytesRequiredForTensor()andTfLiteEvalTensorByteLength()compute tensor byte size usingint element_count, which overflows when model-supplied dimensions are large (e.g., 65536 x 65536 wraps int32 to 0). The undersized allocation leads to heap buffer overflow when kernels later write tensor data using the actual dimensions.Fix
element_countfrominttosize_tkTfLiteErroron overflow or non-positive dimensionsThis replaces the previous PR #3533 which was closed for not following the Error Handling Guide.
Alignment with Error Handling Guide
Per Section 2 (Phase 1: Setup & Initialization):
These functions are called during
InitializeTfLiteTensorFromFlatbuffer(Setup phase). The fix useskTfLiteErrorreturn which callers propagate viaTF_LITE_ENSURE_STATUS, keeping ROM cost low (no additional string literals).Testing
Verified with ASan-enabled build: crafted model with shape [65536, 65536] now returns
kTfLiteErrorinstead of allocating 0 bytes and overflowing.Fixes GHSA-7rx3-jmhh-gq5m